#include "stdafx.h"
#include "base/system/windows_version.h"

namespace xBase
{
    namespace system
    {
        Version GetVersion()
        {
            Version result = VERSION_PRE_XP;

            OSVERSIONINFOEX version_info = { sizeof version_info };
            ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));

            if ((version_info.dwMajorVersion == 5) && (version_info.dwMinorVersion > 0))
            {
                // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
                result = (version_info.dwMinorVersion == 1) ? VERSION_XP : VERSION_SERVER_2003;
            }
            else if (version_info.dwMajorVersion == 6)
            {
                switch (version_info.dwMinorVersion)
                {
                case 0:
                    // Treat Windows Server 2008 the same as Windows Vista.
                    result = VERSION_VISTA;
                    break;
                case 1:
                    // Treat Windows Server 2008 R2 the same as Windows 7.
                    result = VERSION_WIN7;
                    break;
                default:
                    assert(version_info.dwMinorVersion == 2);
                    // Treat Windows Server 2012 the same as Windows 8.
                    result = VERSION_WIN8;
                    break;
                }
            }
            else if (version_info.dwMajorVersion > 6)
            {
                assert(false);
                result = VERSION_WIN_LAST;
            }

            return result;
        }
    }
}